package com.ihmp.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ihmp.business.BlockService;
import com.ihmp.business.FloorService;
import com.ihmp.business.HotelService;
import com.ihmp.business.RoomsBO;
import com.ihmp.model.Tblocks;
import com.ihmp.model.Tfloors;
import com.ihmp.model.Thotels;
import com.ihmp.model.Trooms;

@Controller
public class BookIndividualRoom {
	
	@Autowired
	private HotelService hotelService;
	@Autowired
	private BlockService blockService;
	@Autowired
	private FloorService floorService;
	@Autowired
	private RoomsBO roomsBO;
	public List<Thotels> hotels;
	
	@RequestMapping(value="/accommodation/book-an-individual-room.html",method=RequestMethod.GET)
	public String loadPage(HttpServletRequest request,ModelMap model){		

		hotels = hotelService.list();
		model.addObject("hotels", hotels);
		return "accommodation/book-an-individual-room";				

	}
	
	  @RequestMapping(value="/accommodation/get-roomtype-in-hotel.html",method=RequestMethod.POST)
	    public @ResponseBody String getRoomTypeInHotel(HttpServletRequest request, @RequestParam(value="hotelId", required=true) Integer hotelId ){
		   //   int hotelId = Integer.parseInt(request.getParameter("hotelId")) ;
		     String returnText= "<option selected=\"selected\"></option>";
		     List<Trooms> rooms = roomsBO.getRoomsFromHotel(hotelId);
		     HashMap<Integer, String>  roomtypeMap = new HashMap<Integer,String>();
		    
		    for (Trooms room : rooms) {
		    	roomtypeMap.put(room.getTroomtypes().getRoomTypeId(), room.getTroomtypes().getRoomTypeName());
			}
		    
		    for(Entry<Integer, String> entry : roomtypeMap.entrySet()) {
		        int key = entry.getKey();
		        String value = entry.getValue();
		           returnText+="<option value="+key+">"+value+"</option>";
		      //  returnText="<option value=\"1\">HIHI</option>";
		    }
		    
	        return returnText;
	    }
	  
	  @RequestMapping(value="/accommodation/get-floors-in-hotel.html",method=RequestMethod.POST)
	    public @ResponseBody String getFloorsInHotel(HttpServletRequest request, @RequestParam(value="hotelId", required=true) Integer hotelId ){
		   //   int hotelId = Integer.parseInt(request.getParameter("hotelId")) ;
		     String returnText="<option selected=\"selected\"></option>";
		     List<Tfloors> floors = floorService.getFloorsFromHotelId(hotelId);
		     HashMap<Short, Short>  floorsMap = new HashMap<Short,Short>();
		    
		    for (Tfloors floor : floors) {
		    	floorsMap.put(floor.getFloorId(), floor.getFloorNum());
			}
		    
		    for(Entry<Short, Short> entry : floorsMap.entrySet()) {
		    	Short key = entry.getKey();
		    	Short value = entry.getValue();
		           returnText+="<option value="+key+">"+value+"</option>";
		      //  returnText="<option value=\"1\">HIHI</option>";
		    }
		    
	        return returnText;
	    }
	  
	  @RequestMapping(value="/accommodation/get-blocks-in-hotel.html",method=RequestMethod.POST)
	    public @ResponseBody String getBlocksInHotel(HttpServletRequest request, @RequestParam(value="hotelId", required=true) Integer hotelId ){
		   //   int hotelId = Integer.parseInt(request.getParameter("hotelId")) ;
		     String returnText= "<option selected=\"selected\"></option>";
		     List<Tblocks> blocks = blockService.getBlocksFromHotelId(hotelId);
		     HashMap<Short, Short>  blocksMap = new HashMap<Short,Short>();
		    
		    for (Tblocks block : blocks) {
		    	blocksMap.put(block.getBlockId(), block.getBlockNo());
			}
		    
		    for(Entry<Short, Short> entry : blocksMap.entrySet()) {
		    	Short key = entry.getKey();
		    	Short value = entry.getValue();
		           returnText+="<option value="+key+">"+value+"</option>";
		      //  returnText="<option value=\"1\">HIHI</option>";
		    }
		    
	        return returnText;
	    }
}

